package efg.library.EJB;

import java.util.HashMap;
import java.util.HashSet;

public class Customer
{   
  /***************************************************************************\
   * Static
  \***************************************************************************/  
  private static HashMap<String, Customer> customers = new HashMap<String, Customer>();
  
  static
  {
    new Customer("Eric");
    new Customer("Fred");
    new Customer("John");
    new Customer("Henk");
  } 
  
  static Customer findCustomer(String name)
  {
    synchronized (customers)
    {
      return customers.get(name);
    }
  }
  
  static void removeCustomer(String name)
  {
    synchronized (customers)
    {
      customers.remove(name);
    }
  }
  
  static HashSet<Customer> allCustomers()
  {
    HashSet<Customer> ret = new HashSet<Customer>();
    synchronized (customers)
    {
      for (String name: customers.keySet())
      {
        ret.add(customers.get(name));
      }
    }
    return ret;
  }
  /***************************************************************************\
   * Object
  \***************************************************************************/  
  private String name = null;
  private int credits = 0;
  private HashSet<Book> books = new HashSet<Book>();
  
  Customer(String newName)
  {
    name = newName;
    synchronized (customers)
    {
      customers.put(name, this);
    }
  }
  
  public String getName()
  {
     return name;
  }
  
  int getCredits()
  {
    return credits;
  }
  
  void setCredits(int newCredits)
  {
    credits = newCredits;
  }
  
  HashSet<Book> getBooks()
  {
    return books;
  }
  
  void addBook(Book book)
  {
    books.add(book);
  }
  
  void removeBook(Book book)
  {
    books.remove(book);
  }
  
  @Override
  public String toString()
  {
    String ret =  "[Customer: "+name;
    for (Book book : books)
    {
      ret += (" "+book.getTitle());
    }
    return "customer"; //ret +"]";
  }
}
